package com.example.homelicaisystem.control;


import com.example.homelicaisystem.entity.*;
import com.example.homelicaisystem.entity.DTO.UserDTO;
import com.example.homelicaisystem.services.FamilyService;
import com.example.homelicaisystem.services.UserService;
import com.example.homelicaisystem.utils.HandleMoney;
import com.example.homelicaisystem.utils.Result;
import com.example.homelicaisystem.utils.UserUtil;
import com.github.pagehelper.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.validation.Valid;
import java.util.List;

@RequestMapping("/user")
@RestController
@Slf4j
public class UserControl {

    private final UserService userService;
    private final FamilyService familyService;

    public UserControl(UserService userService, FamilyService familyService) {
        this.userService = userService;
        this.familyService = familyService;
    }

    @PostMapping("/list")
    public Result<ListWithCount<User>> getList(@RequestBody UserDTO data) {
        log.debug("收到请求list请求。参数:{}", data);
        Page<User> res = userService.getList(data);
        System.out.println(res);
        return Result.ok(new ListWithCount<>(res.getTotal(), res));
    }

    @GetMapping("/select")
    public Result<List<SelectResult>> select() {
        log.debug("收到select请求。");
        return Result.ok(userService.select());
    }

    @DeleteMapping("/delete/{id}")
//    没有返回值，泛型用？
    public Result<?> delete(@PathVariable int id) {
        log.debug("收到delete请求。");
        userService.delete(id);

        //删除用户的权限
        userService.deleteUserPower(id);

        return Result.ok(null);
    }

    @PatchMapping("/modify")
    public Result<?> modify(@RequestBody User data) {
        log.debug("收到modify请求。参数:{}", data);
        //拿到后台处理时，金钱单位为最小单位 分   用户支付宝
        data.setAlipay(HandleMoney.backendMoney(data.getAlipay()));
        //拿到后台处理时，金钱单位为最小单位 分   用户微信
        data.setWeichat(HandleMoney.backendMoney(data.getWeichat()));
        userService.modify(data);
        return Result.ok(null);
    }

    @PostMapping("/create")
    public Result<Integer> create(@Valid @RequestBody User data) {
        log.debug("收到create请求。参数:{}", data);

        userService.create(data);

        User user = userService.getUserByPhone(data.getPhone());

        //初始化创建用户的权限
        userService.initializeUserPower(user);

        return Result.ok(data.getId());
    }

    @PostMapping("/login")
    public Result<?> login(@RequestBody User data) {
        log.debug("收到login请求。参数:{}", data);
        userService.login(data);

        User user = userService.getUserByPhone(data.getPhone());


        return Result.ok(UserUtil.getToken(user.getId()));
    }

    @PostMapping("/register")
    public Result<?> register(@RequestBody User data) {
        log.debug("收到register请求。参数:{}", data);

        userService.create(data);
        User user =  userService.getUserByPhone(data.getPhone());
        //创建家庭
        familyService.createFamily(user);

        int familyId =familyService.getFamilyIdByUserId(user.getId());
        user.setFamilyId(familyId);

        //初始化创建用户的权限
        userService.initializeUserPower(user);

        userService.modify(user);
        return Result.ok(data);
    }

    @GetMapping("getPower")
    public Result<List<Power>> getPower() {
        log.debug("收到getPower请求。");
        return Result.ok(userService.getPower());
    }


}
